package com.blog.rabbitmqdemo.topic;

import com.blog.rabbitmqdemo.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Producer {
    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        String exchangeName = "test_topic";
        // 创建交换机
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC, true, false, false,null);
        // 创建队列
        String queue1Name = "test_topic_queue1";
        String queue2Name = "test_topic_queue2";
        // 声明队列
        channel.queueDeclare(queue1Name, true, false, false, null);
        channel.queueDeclare(queue2Name, true, false, false, null);

        //  绑定队列和交换机
        channel.queueBind(queue1Name, exchangeName, "#.error");
        channel.queueBind(queue1Name, exchangeName, "order.*");
        channel.queueBind(queue2Name, exchangeName, "*.*");

        String body = null;
//        body = "[所在系统：order][日志级别：info][日志内容：订单生成，保存成功]";
//        channel.basicPublish(exchangeName, "order.info", null, body.getBytes());

//        body = "[所在系统：goods][日志级别：error][日志内容：商品发布成功]";
//        channel.basicPublish(exchangeName, "goods.info", null, body.getBytes());

        body = "[所在系统：goods][日志级别：error][日志内容：商品发布失败]";
        channel.basicPublish(exchangeName, "goods.error", null, body.getBytes());

        // 7.释放资源
        channel.close();
        connection.close();
    }
}
